package CustomClassFile.CronJob;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	/**
	 * 获取当前时间
	 * "yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String currentDateString() {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=now.getTime();
		String dateStr=sdf.format(date);
		return dateStr;
	}
	
	/**
	 * 获取当期时间 仅到分钟
	 * yyyy年MM月dd日 HH:mm
	 * @return
	 */
	public static String currentDateStringlocal() {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
		Date date=now.getTime();
		String dateStr=sdf.format(date);
		return dateStr;
	}
	
	public static String dateString(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr=sdf.format(date);
		return dateStr;
	}
	
	public static String dateString(Date date,String patten) {
		if (patten==null) {
			patten="yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(patten);
		String dateStr=sdf.format(date);
		return dateStr;
	}
	 /** 
     * 时间戳转换成日期格式字符串 
     * @param seconds 精确到秒的字符串 
     * @param formatStr 
     * @return 
     */  
    public static String timeStamp2Date(String seconds,String format) {  
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){  
            return "";  
        }  
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }   
        SimpleDateFormat sdf = new SimpleDateFormat(format);  
        return sdf.format(new Date(Long.valueOf(seconds+"000")));  
    }  
    
    /** 
     * 日期格式字符串转换成时间戳 
     * @param date 字符串日期 
     * @param format 如：yyyy-MM-dd HH:mm:ss 
     * @return 
     */  
    public static String date2TimeStamp(String date_str,String format){  
    	  if(format == null || format.isEmpty()){
              format = "yyyy-MM-dd HH:mm:ss";
          } 
        try {  
            SimpleDateFormat sdf = new SimpleDateFormat(format);  
            return String.valueOf(sdf.parse(date_str).getTime()/1000);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return "";  
    }  
    
    /** 
     * 日期格式字符串转换成时间
     * @param date 字符串日期 
     * @param format 如：yyyy-MM-dd HH:mm:ss 
     * @return 
     */  
    public static Date StringToDate(String date_str,String format){  
    	  if(format == null || format.isEmpty()){
              format = "yyyy-MM-dd HH:mm:ss";
          } 
        try {  
            SimpleDateFormat sdf = new SimpleDateFormat(format);  
            return sdf.parse(date_str);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    } 
      
    /** 
     * 取得当前时间戳（精确到秒） 
     * @return 
     */  
    public static String currenttimeStamp(){  
        long time = System.currentTimeMillis();
        String t = String.valueOf(time/1000);  
        return t;  
    }  
    /**
     * 	判断 当前时间  是否超时 超时true
     * @param create_time
     * @param expiresIn
     * @return
     */
    public static boolean isOvertime(Date create_time,Integer expiresIn) {
    	 long current_time = System.currentTimeMillis()/1000;
    	 long intime= create_time.getTime()+(long)expiresIn;
    	 if (current_time<intime) {
			return false;
		}
    	 return true;
    }
    /**
     *	 时间对分钟运算
     * @param date
     * @param minute  分钟
     * @return
     */
    public static Date dateCalculate(Date date,int minute) {
    	 Calendar rightNow = Calendar.getInstance();
    	 rightNow.setTime(date);
         rightNow.add(Calendar.MINUTE,minute);//时间加1分钟
         Date dateResult=rightNow.getTime();
         return dateResult;
    }
    /**
     * 	当前时间对分钟运算,返回Date
     * @param minute
     * @return
     */
	public static Date currentDateCalculate(int minute) {
		Calendar rightNow = Calendar.getInstance();
		rightNow.add(Calendar.MINUTE, minute);// 时间加1分钟
		Date dateResult = rightNow.getTime();
		return dateResult;
	}
	
	/**
     * 	时间对分钟运算,并返回字符串
     * @param minute
     * @return
     */
	public static String dateCalculateStr(Date date,int minute) {
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date);
		rightNow.add(Calendar.MINUTE, minute);// 时间加1分钟
		Date dateResult = rightNow.getTime();
		return dateString(dateResult);
	}
	
	/**
     * 	日期对天数运算,并返回字符串
     * @param minute
     * @return
     */
	public static String datedayCalculateStr(Date date,int days) {
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date);
		rightNow.add(Calendar.DAY_OF_MONTH, days);// 
		Date dateResult = rightNow.getTime();
		return dateString(dateResult,"yyyy-MM-dd");
	}
	
	/**
     *	 日期对月份运算,并返回字符串
     * @param minute
     * @return
     */
	public static String dateMonthCalculateStr(Date date,int months) {
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date);
		rightNow.add(Calendar.MONTH, months);// 
		Date dateResult = rightNow.getTime();
		return dateString(dateResult,"yyyy-MM-dd");
	}
	/**
     *	 日期对月份运算,并返回字符串
     * @param minute
     * @return
     */
	public static Date dateMonthCalculate(Date date,int months) {
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(date);
		rightNow.add(Calendar.MONTH, months);// 
		Date dateResult = rightNow.getTime();
		return dateResult;
	}
	/**
	 * 判断字符串日期大小，Date1>Date2  返回1
	 * @param DATE1
	 * @param DATE2
	 * 
	 * "yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public static int compare_date(String DATE1, String DATE2) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 判断字符串日期大小，Date1>Date2  返回1
	 * @param DATE1
	 * @param DATE2
	 * 
	 * "yyyy-MM-dd HH:mm:ss"
	 * @return
	 */
	public static int compare_date(String DATE1, String DATE2,String pattern) {
		if(pattern==null) {
			pattern="yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		try {
			
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}
	//获取当前时间，精确到分钟
	public static String getCurrentDateTime(String format) {
		if (format==null) {
			format="yyyy-MM-dd HH:mm:ss";
		}
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		return sdf.format(date);
	}
	public static void main(String[] args) {
		int i=DateUtil.compare_date(DateUtil.getCurrentDateTime("HH:mm:ss"), 
				"23:30:00","HH:mm:ss");
		System.out.println(i);
		System.out.println(getMonthLastDay(2018, 2));
	}
	
	/**
	 * 取得当月天数
	 * */
	public static int getCurrentMonthLastDay()
	{
		Calendar a = Calendar.getInstance();
		a.set(Calendar.DATE, 1);//把日期设置为当月第一天
		a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
		int maxDate = a.get(Calendar.DATE);
		return maxDate;
	}

	/**
	 * 得到指定月的天数
	 * */
	public static int getMonthLastDay(int year, int month)
	{
		Calendar a = Calendar.getInstance();
		a.set(Calendar.YEAR, year);
		a.set(Calendar.MONTH, month - 1);
		a.set(Calendar.DATE, 1);//把日期设置为当月第一天
		a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
		int maxDate = a.get(Calendar.DATE);
		return maxDate;
	}
}

